package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;


    @Transactional
    @Override
    public void addDish(DishDTO dto) {
        // 新增菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.addDish(dish);
        // 新增口味
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors == null || flavors.size() == 0) {
            return;
        }
        // 设置所属菜品的ID
        flavors.forEach(flavor -> {
            flavor.setDishId(dish.getId());
        });
        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public PageResult page(DishPageQueryDTO dto) {
        // 1.设置分页参数
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        // 2.查询数据
        List<DishVO> list = dishMapper.list(dto);
        Page<DishVO> page = (Page<DishVO>) list;
        // 3.返回数据
        return new PageResult(page.getTotal(),page.getResult());

    }

    @Transactional
    public void delete(List<Long> ids) {
        // 1.已经上架的菜品不能删除
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // 2.套餐内的菜品不能删除
        Integer counts = setmealDishMapper.countByDishIds(ids);
        if (counts > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 3.删除菜品
        dishMapper.deleteBatch(ids);
        // 4.删除菜品口味
        dishFlavorMapper.deleteBatch(ids);
    }

    public DishVO selectById(Long id) {
        DishVO vo = new DishVO();
        // 1.查询菜品数据
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, vo);
        // 2.查询菜品口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectByDishId(id);
        vo.setFlavors(dishFlavors);
        return vo;
    }

    @Transactional
    @Override
    public void updateDish(DishDTO dto) {
        // 更新菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updateDish(dish);
        // 更新菜品口味
        dishFlavorMapper.deleteByDishId(dto.getId());
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public List<DishVO> list(DishPageQueryDTO dto) {

        List<DishVO> result = dishMapper.list(dto);
        return result;
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        DishPageQueryDTO dto = new DishPageQueryDTO();
        BeanUtils.copyProperties(dish, dto);
        List<DishVO> dishVOList = dishMapper.list(dto);


        for (DishVO dishVO : dishVOList) {
            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(dishVO.getId());
            dishVO.setFlavors(flavors);
        }

        return dishVOList;
    }
}
